package com.daqsoft.service.process;

import com.daqsoft.data.User;
import com.daqsoft.data.project.Project;
import com.daqsoft.data.project.ProjectEdition;
import com.daqsoft.data.project.Task;
import com.daqsoft.dto.StatusEnum;
import com.daqsoft.dto.TaskTypeEnum;
import com.daqsoft.service.project.ProjectEditionService;
import com.daqsoft.service.project.ProjectService;
import com.daqsoft.service.project.TaskService;
import com.daqsoft.util.DateUtil;
import com.daqsoft.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 项目变更流程服务层
 * @author 周麟东 on 13:57
 */
@Service
public class ProjectChangeProcessService {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectEditionService projectEditionService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ProjectConfimProcessService projectConfimProcessService;

    /**
     * 创建变更项目
     *
     * @param previousId 上个版本项目的ID
     * @return 当前项目对象
     */
    @Transactional
    public Project createChangeProject(int previousId) {
        //先保存新建的项目
        Project previous = projectService.get(previousId);
        //获取最终版本
        previous = projectService.getLastVersion(previous);
        Project current = projectService.copyProject(previous);
        current = projectService.save(current);
        //保存原始项目的状态
        if (previous.getStatus() == StatusEnum.CAN_CHANGE){
            previous.setStatus(StatusEnum.PROCESS);
        }
        if(previous.getStatus() == StatusEnum.HIDDEN_CAN_CHANGE){
            previous.setStatus(StatusEnum.HIDDEN);
        }
        projectService.save(previous);
        //建立关系 , 把原始版本项目查询出来
        Project initial = projectEditionService.getInitial(previous);

        //如果原始版本为空则意味着上个版本 为原始版本
        if (initial == null) {
            initial = previous;
        }
        //将版本关系保存到数据库
        ProjectEdition edition = projectEditionService.getByCurrent(current);
        if (edition == null) {
            edition = new ProjectEdition();
        }
        edition.setCurrent(current);
        edition.setInitial(initial);
        edition.setPrevious(previous);
        edition.setUpdateTime(new Date());

        projectEditionService.save(edition);
        return current;
    }

    /**
     * 创建变更项目审核任务
     *
     * @param project   项目
     * @param verifier  负责人
     * @param principal 责任人
     */
    @Transactional
    public void createChangeProjectTask(Project project, User verifier, User principal) {

        ProjectEdition projectEdition = projectEditionService.getByCurrent(project);
        Task task = new Task();
        task.setName("项目变更审核");
        Project guishu = projectService.getLastUnHiddenVersion(projectEdition.getPrevious());
        if (guishu == null){
            guishu = projectEdition.getPrevious();
        }
        task.setMsg(Log.format(principal, "<i>#创建项目变更任务#</i>"));
        task.setProject(guishu);//这个任务属于之前的没有被隐藏的项目
        task.setType(TaskTypeEnum.CHANGE_CHECK);
        task.setStatus(StatusEnum.CHECK);
        task.setQuestion(projectEdition.getPrevious().getId() + "");//这里问题指最初的项目
        task.setSolution(project.getId() + "");//这里解决方案指的是当前项目的ID
        task.setVerifier(verifier);
        task.setPrincipal(principal);
        task.setStartDate(new Date());
        task.setEndDate(DateUtil.INS.addDay(new Date(), 3));
        task.setRealStartDate(new Date());
        taskService.save(task);
        //更改项目状态
        project.setStatus(StatusEnum.CHANGED);
        projectService.save(project);
    }
    /**
     * 审核变更后的项目
     * @param project 变更后的项目
     * @param task 任务
     * @param pass 是否审核完成
     */
    @Transactional
    public void checkChangeProject(Project project, Task task , boolean pass) {
        ProjectEdition projectEdition = projectEditionService.getByCurrent(project);
        Project prev = projectEdition.getPrevious();

        if (!pass){
            task.setStatus(StatusEnum.ERROR);
            taskService.save(task);
            //隐藏 留底 项目为可变更
            project.setStatus(StatusEnum.HIDDEN_CAN_CHANGE);
            projectService.save(prev);
            return;
        }
        //审核通过
        Project p  = projectService.getLastUnHiddenVersion(prev);
        if (p == null){
            p = prev;
        }
        p.setStatus(StatusEnum.HIDDEN);
        projectService.save(p);
        //走提交任务流程
        projectConfimProcessService.submitProject(project);
        //设置审核任务完成
        task.setEndDate(new Date());
        task.setRealEndDate(new Date());
        task.setStatus(StatusEnum.CHECKED);

    }

}
